<?php 
/**
 * [weliam] Copyright (c) 2016/3/26
 * 商城系统设置控制器
 */
defined('IN_IA') or exit('Access Denied');

load()->func('communication');
$ops = array('qr','ajax','copyright');
$op_names = array('系统设置');
foreach($ops as $key=>$value){
	permissions('do', 'ac', 'op', 'store', 'setting', $ops[$key], '订单', '参数设置', $op_names[$key]);
}
$op=$_GPC['op'];
$op = in_array($op, $ops) ? $op : 'copyright';
wl_load()->model('setting');
$acct = pdo_fetch("select * from " . tablename('account_wechats') . " where  uniacid = '{$_W['uniacid']}'");
//权限控制
$tid=8169;
//
wl_load()->model('functions');
$checkfunction=checkfunc($tid);
$isdeliverytype=checkfunc(8155);//运费模板
	$issh=checkfunc(8157);//送货
	$iszt=checkfunc(8156);//自提
$_W['page']['title'] = $checkfunction['name'];
if($op=='qr')
{
	$functionid=$_GPC['id'];
	$num=$_GPC['num'];
	$orderno=$_GPC['orderno'];
	$uniacid=$_GPC['uniacid'];
	/*二维码*/
		wl_load()->classs('qrcode');
		$createqrcode =  new creat_qrcode();
		$createqrcode->createfunctionpayQrcode($functionid,$num,$orderno,$uniacid);	
	die(json_encode(array('errno'=>0,'message'=>$functionid)));
}
if($op=='ajax')
{	
	$id=$_GPC['id'];
	$name=$_GPC['name'];
	pdo_update("account_wechats",array('tpl'=>$id),array('uniacid'=>$_W['uniacid']));
	die(json_encode(array('errno'=>0,'message'=>$name."已成功启用")));
}
if ($op == 'copyright') {
	$_W['page']['title'] = '商城信息设置 - 系统管理';
	$set = setting_get_list();
	if(empty($set)){
		$settings = $this->module['config'];
	}else{
		$settings = array();
		foreach($set as$key=>$value){
			$settingarray= $value['value'];
			foreach($settingarray as $k=>$v){
				$settings[$k] = $v;
			}
		}
	}
	$functionlist=pdo_fetchall("select * from ".tablename('tg_function')." where type=1");
	$wechats=pdo_fetch("select * from ".tablename('account_wechats')." where uniacid=".$_W['uniacid']);
	foreach($functionlist as $key=>$value){
			$functionlist[$key]['status']=0;
			$functionlist[$key]['endtime']=0;
			$function_detail=pdo_fetch("select * from ".tablename('tg_function_detail')." where functionid=:functionid and  uniacid=:uniacid",array(':functionid'=>$value['id'],':uniacid'=>$_W['uniacid']));
			
			if(!empty($function_detail)&&$function_detail['endtime']>time())
			{
				$functionlist[$key]['status']=1;
				$functionlist[$key]['endtime']=$function_detail['endtime'];
			}
			if((($acct['vip']==1&&$acct['endtime']>time())||$acct['ordernum']>0)&&$value['isdingzhi']==0)
			{		
				$functionlist[$key]['status']=1;
				$functionlist[$key]['endtime']=$acct['endtime'];
				if($acct['ordernum']>0)
				{
					$functionlist[$key]['endtime']=1606492800;
				}
			}
			if($wechats['tpl']==$value['id'])
			{
				$functionlist[$key]['status']=2;
			}
	}
	$storesids = explode(",", $settings['hexiao_id']);
	foreach($storesids as$key=>$value){
		if($value){
			$stores[$key] =  pdo_fetch("select * from".tablename('tg_store')."where id ='{$value}' and uniacid='{$_W['uniacid']}'");
		}
	}
	$yunfeiids = explode(",", $settings['yunfei_id']);
	foreach($yunfeiids as$key=>$value){
		if($value){
			$dispatch_list[$key] =  pdo_fetch("select * from".tablename('tg_delivery_template')."where id ='{$value}' and uniacid='{$_W['uniacid']}'");
		}
	}
//    if ($_W['uniacid'] == 4) {
//        wl_debug($settings);
//    }
	if (checksubmit('submit')) {
		load()->func('file');
        $r = mkdirs(IA_ROOT . '/attachment/lexiangpingou/cert/'. $_W['uniacid']);
		$r2 = mkdirs(TG_CERT.$_W['uniacid']);
		if(!empty($_GPC['cert'])) {
            $ret = file_put_contents(IA_ROOT . '/attachment/lexiangpingou/cert/'.$_W['uniacid'].'/apiclient_cert.pem', trim($_GPC['cert']));
            $ret2 = file_put_contents(TG_CERT.$_W['uniacid'].'/apiclient_cert.pem', trim($_GPC['cert']));
            $r = $r && $ret;
        }
        if(!empty($_GPC['key'])) {
            $ret = file_put_contents(IA_ROOT . '/attachment/lexiangpingou/cert/'.$_W['uniacid'].'/apiclient_key.pem', trim($_GPC['key']));
            $ret2 = file_put_contents(TG_CERT.$_W['uniacid'].'/apiclient_key.pem', trim($_GPC['key']));
            $r = $r && $ret;
        }
		if(!empty($_GPC['rootca'])) {
            $ret = file_put_contents(IA_ROOT . '/attachment/lexiangpingou/cert/'.$_W['uniacid'].'/apiclient_rootca.pem', trim($_GPC['rootca']));
            $ret2 = file_put_contents(TG_CERT.$_W['uniacid'].'/apiclient_rootca.pem', trim($_GPC['rootca']));
            $r = $r && $ret;
        }
		if(!$r) {
            message('证书保存失败, 请保证该目录可写');
        }
		if(intval($_GPC['order_alert_time'])==0)
		{
			$order_alert_time=5;
		}else{
			$order_alert_time=intval($_GPC['order_alert_time']);
		}
		//核销ID
		$store = $_GPC['storeids'];
		$str='';
		if($store){
			foreach($store as $key => $value){
			$str.=$value.",";
		}
		}
		//运费ID
		$dispatchs = $_GPC['dispatchs'];
		$str1='';
		if($dispatchs){
			foreach($dispatchs as $key => $value){
			$str1.=$value.",";
		}
		}
		
		$base = array(
			'bukuan_status'=>$_GPC['bukuan_status'],
			'guanzhu'=>$_GPC['guanzhu'],
			'goodstip'=>$_GPC['goodstip'],
			'sharestatus' => $_GPC['sharestatus'],
			'order_alert'=>$_GPC['order_alert'],
			'iszhe'=>$_GPC['iszhe'],
			'ismarketprice'=>$_GPC['ismarketprice'],
			'issell'=>$_GPC['issell'],
			'order_alert_time'=>$order_alert_time,
			'service'=>$_GPC['service'],
			'job'=>$_GPC['job'],
			'guess'=>$_GPC['guess'],
			'firstfree'=>$_GPC['firstfree'],
			'over_free'=>$_GPC['over_free'],
			'search' => $_GPC['search'],
			'meiqia' => $_GPC['meiqia'],
			'deliverytype' => $_GPC['deliverytype'],
			'gettime' => $_GPC['gettime'],
			'hexiao_id'=>$str,
			'yunfei_id'=>$str1,
			'cancle_time' => $_GPC['cancle_time'],
			'noprice_time' => $_GPC['noprice_time'],
			'share_type'=>$_GPC['share_type']
		);
		$share = array(
			'share_title' => $_GPC['share_title'],
            'share_image' => $_GPC['share_image'],
            'share_desc' => $_GPC['share_desc']
		);
		$refund = array(
			'mchid' => $_GPC['mchid'],
			'apikey' => $_GPC['apikey'],
			'auto_refund'=>$_GPC['auto_refund']
		);
		$acctdata = array(			
            'homeimg'=>$_GPC['homeimg']
		);
		$tginfo = array(
			'sname'=>$_GPC['sname'],
            'slogo'=>$_GPC['slogo'],            
            'guanzhu'=>$_GPC['guanzhu'],
            'qrcode'=>$_GPC['qrcode'],
			'kefu_image'=>$_GPC['kefu_image'],
            'followed_image'=>$_GPC['followed_image'],
            'content' => htmlspecialchars_decode($_GPC['content'])
		);
		$style = array(
			'appview'=>$_GPC['appview'],
			'webview'=>$_GPC['webview']
		);

		$tip = array(
			'tag4'=>$_GPC['tag4'],
            'tag3'=>$_GPC['tag3'],
            'tag2'=>$_GPC['tag2'],
            'tag1'=>$_GPC['tag1']
		);
		
		$pdobase = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'base',
			'value'=>serialize($base)
		);
		$pdoshare = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'share',
			'value'=>serialize($share)
		);
		$pdorefund = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'refund',
			'value'=>serialize($refund)
		);
		$pdomessage = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'message',
			'value'=>serialize($message)
		);
		$pdotginfo  = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'tginfo',
			'value'=>serialize($tginfo)
		);
		$pdotip = array(
			'uniacid'=>$_W['uniacid'],
			'key'=>'tip',
			'value'=>serialize($tip)
		);
		tgsetting_save($style, 'style');
		$ifbase = setting_get_by_name('base');
		$ifshare = setting_get_by_name('share');
		$ifrefund = setting_get_by_name('refund');
		$ifmessage = setting_get_by_name('message');
		$iftginfo = setting_get_by_name('tginfo');
		$iftip = setting_get_by_name('tip');
		if(!empty($ifbase)){
			setting_update_by_params($pdobase, array('key'=>'base','uniacid'=>$_W['uniacid']));
		}else{
			setting_insert($pdobase);
		}
		if(!empty($ifshare)){
			setting_update_by_params($pdoshare, array('key'=>'share','uniacid'=>$_W['uniacid']));
		}else{
			setting_insert($pdoshare);
		}
		if(!empty($ifrefund)){
			setting_update_by_params($pdorefund, array('key'=>'refund','uniacid'=>$_W['uniacid']));
		}else{
			setting_insert($pdorefund);
		}
		
		if(!empty($iftginfo)){
			setting_update_by_params($pdotginfo, array('key'=>'tginfo','uniacid'=>$_W['uniacid']));
		}else{
			setting_insert($pdotginfo);
		}
		if(!empty($iftip)){
			setting_update_by_params($pdotip, array('key'=>'tip','uniacid'=>$_W['uniacid']));
		}else{
			setting_insert($pdotip);
		}
		if(!empty($_GPC['homeimg']))
		{
			pdo_update('account_wechats', $acctdata, array('uniacid' => $_W['uniacid']));
		}
		if(!empty($_GPC['secret']))
		{
			pdo_update('account_wechats', array('secret'=>$_GPC['secret']), array('uniacid' => $_W['uniacid']));
		}
		$tip='更新设置成功！';
		echo "<script>alert('".$tip."');location.href='".web_url('store/setting/copyright')."';</script>";
		exit;
	}
}

include wl_template('store/setting');
exit();